<template>
  <div class="dialog">
    <el-dialog
      title="绑定教师"
      :visible.sync="isShowBindTeacherDialog"
      width="50%"
      :before-close="handleBindTeacherClose">
      <el-form  v-loading="loading"
                element-loading-text="拼命加载中......"
                element-loading-spinner="el-icon-loading" >
        <div class="dialog-box">
          <span class="bind-title">绑定教师 :</span>
          <el-select v-model="form.BindingID" placeholder="请选择教师">
            <el-option
              v-for="(item,index) in teacherList"
              :key="index"
              :label="item.Text"
              :value="item.Value">
            </el-option>
          </el-select>
        </div>

      </el-form>


      <span slot="footer" class="dialog-footer">
    <el-button @click="handleBindTeacherClose" :disabled="loading">取 消</el-button>
    <el-button type="primary" @click="submitEvent" :disabled="loading">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
//获取教师信息
import TeachersManage from '../../../model/teachersManage'
//绑定教师
import CompanyCurriculumDetails from '../../../model/companyCurriculumDetails'

export default {
  name: 'BindTeacherDialog',
  props: {
    isShowBindTeacherDialog: Boolean,
    bindTeacherId: Number,
  },
  data () {
    return {
      loading:false,
      teacherList: [],
      form: {
        ID: '',
        BindingID: ''
      }
    }
  },
   created () {
     this.getTeacherList()
     this.showInfo()
  },
  methods: {
    handleBindTeacherClose (payload) {
      this.$emit('handleBindTeacherClose', payload)
    },
    //获取教师集合信息
    async getTeacherList () {
      const res = await TeachersManage.PageSearchList()
      this.teacherList = res
    },
    //提交数据
    async submitEvent () {
      if (this.form.BindingID === '') {
        this.$message.error('绑定教师为必选项')
      } else {
        try {
          this.loading=true
          this.form.ID = this.bindTeacherId
          await CompanyCurriculumDetails.BingdingTeacher(this.form)
          this.loading=false
          this.handleBindTeacherClose('submitButton')
        } catch (err) {
        }
      }
    },
    //当有数据的时候展示已选中的数据
   async showInfo(){
      this.form.ID=this.bindTeacherId
      const res = await CompanyCurriculumDetails.SearchModelDetails(this.form)
      this.form.BindingID=res.TeacherID
    }
  }
}
</script>

<style scoped>
.bind-title {
  line-height: 40px;
  width: 80px;
}

.dialog-box {
  display: flex;
}
</style>
